home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 3_16.lha / 3_16 / 3_16.in < prev    next >
Text File  |  1993-08-08  |  2KB  |  112 lines

  1. include <stream.h>
  2. xtern void error(char *);
  3.  
  4. include "3_16b.c"        /* dolinecomment */
  5. include "3_16c.c"        /* doblockcomment */
  6. include "3_16d.c"        /* do_string_or_char */
  7. include "3_16a.c"        /* main */
  8. / Strip all C++ comments
  9. / from a source file read
  10. / on the standard input.
  11. include <stream.h>
  12.  
  13. nt main(int, char**)
  14.  
  15.    char ch;
  16.  
  17.    while (cin.get(ch))
  18. {
  19. // a / may be followed by another / or a *
  20. if (ch == '/')
  21.     {
  22.     cin.get(ch);
  23.     if (!cin)
  24.     break;
  25.  
  26.     if (ch == '/')    dolinecomment();
  27.     else if (ch == '*')    doblockcomment();
  28.  
  29.     // if not one of the above, then it's
  30.     // not a comment and the 2nd character
  31.     // must be rescanned
  32.     else
  33.     {
  34.     cout.put('/');
  35.     cin.putback(ch);
  36.     }
  37.     }
  38.  
  39. else            // a printable character
  40.     {
  41.     cout.put(ch);
  42.     if (ch == '"' || ch == '\'')
  43.     do_string_or_char(ch);
  44.     }
  45. }
  46.    return 0;
  47.  
  48. * handle a line comment */
  49. oid dolinecomment()
  50.  
  51.    char ch;
  52.  
  53.    // loop until end of line
  54.    while (cin.get(ch))
  55. if (ch == '\n')
  56.     {
  57.     cout.put(ch);
  58.     return;
  59.     }
  60.  
  61.    error("EOF found within line comment");
  62.  
  63. * handle a block comment */
  64. oid doblockcomment()
  65.  
  66.    cout.put(' ');
  67.    char ch;
  68.  
  69.    // loop until matching */
  70.    while (cin.get(ch))
  71. if (ch == '*')
  72.     {
  73.     cin.get(ch);
  74.     if (ch == '/')
  75.     return;
  76.     cin.putback(ch);
  77.     }
  78.  
  79. else if (ch == '\n')
  80.     cout.put(ch);
  81.  
  82.    error("EOF found within full comment");
  83.  
  84. * Copy a string or character constant. */
  85. * Startquote is the character, either */
  86. * ' or ", which started this constant. */
  87. oid do_string_or_char(char startquote)
  88.  
  89.    char ch;
  90.  
  91.    while (cin.get(ch))
  92. {
  93. // copy chars within string
  94. cout.put(ch);
  95.  
  96. // found 2nd quote?
  97. if (ch == startqΓnt"
  98.    return;
  99.  
  100. // found \x, skip past 1 char
  101. if (ch == '\\')
  102.     {
  103.     cin.get(ch);
  104.     if (!cin)
  105.     break;
  106.     cout.put(ch);
  107.     }
  108. }
  109.  
  110.    error("EOF found within string or char constant");
  111.  
  112.